<template>
  <div class="pay-success">
    <!--如果是单独的页面，就没必要展示导航栏(带有登录的用户)-->
    <Header/>
    <div class="main">
      <div class="title">
        <div class="success-tips">
          <p class="tips">购买成功！</p>
        </div>
      </div>
      <div class="order-info">
        <p class="info"><b>订单号：</b><span>{{ result.out_trade_no }}</span></p>
        <p class="info"><b>交易号：</b><span>{{ result.trade_no }}</span></p>
        <p class="info"><b>付款时间：</b><span><span>{{ result.timestamp }}</span></span></p>
      </div>
    </div>
  </div>
</template>

<script>
import Header from "@/components/Header"

export default {
  name: "PaySuccess",
  data() {
    return {
      result: {},
      cardname: '',
      cardno: '',
      daynum: '',
    };
  },
  created() {
    let params = location.search.substring(1);
    let items = params.length ? params.split('&') : [];
    for (let i = 0; i < items.length; i++) {
      let k_v = items[i].split('=');

      if (k_v.length >= 2) {

        let k = decodeURIComponent(k_v[0]);
        this.result[k] = decodeURIComponent(k_v[1]);

      }

    }
    this.cardno = this.result.out_trade_no
    this.$http({
      url: this.$settings.base_url + 'order/success/' + location.search,
      method: 'get',
    }).then(response => {
      console.log(response.data);
    }).catch(() => {
      console.log('支付结果同步失败');
    });

    this.$http.post(`${this.$settings.base_url}order/vipnum/`, {out_trade_no: this.cardno}).then(res2 => {
      this.daynum = res2.data.msg['num']
      this.cardname = res2.data.msg['cardname']
    });

    this.$http.get(`${this.$settings.base_url}vip/vipuser/?username=` + this.$cookies.get('username'),
        {
          headers: {
            Authorization: 'jwt ' + this.$cookies.get('token')
          }
        }).then(res => {
      if (res.data.length!=0) {
        this.cardno = res.data[0]['cardno']
        this.$http.put(`${this.$settings.base_url}vip/vipuser/1`, {
          cardno: this.cardno,
          daynum: this.daynum
        }, {
          headers: {
            Authorization: 'jwt ' + this.$cookies.get('token')
          }
        }).then(res => {
          this.$message({
            message: res.data.msg
          });
        })
      } else {
        this.$http.post(`${this.$settings.base_url}vip/vipuser/`, {
          username: this.$cookies.get('username'),
          cardname: this.cardname,
          daynum: this.daynum
        }, {
          headers: {
            Authorization: 'jwt ' + this.$cookies.get('token')
          }
        }).then(res => {
          this.$message({
            message: res.data.msg
          })
        })
      }
    })
  },
  components: {
    Header,

  }
}
</script>

<style scoped>
.main {
  padding: 60px 0;
  margin: 0 auto;
  width: 1200px;
  background: #fff;
}

.main .title {
  display: flex;
  -ms-flex-align: center;
  align-items: center;
  padding: 25px 40px;
  border-bottom: 1px solid #f2f2f2;
}

.main .title .success-tips {
  box-sizing: border-box;
}

.title img {
  vertical-align: middle;
  width: 60px;
  height: 60px;
  margin-right: 40px;
}

.title .success-tips {
  box-sizing: border-box;
}

.title .tips {
  font-size: 26px;
  color: #000;
}


.info span {
  color: #ec6730;
}

.order-info {
  padding: 25px 48px;
  padding-bottom: 15px;
  border-bottom: 1px solid #f2f2f2;
}

.order-info p {
  display: -ms-flexbox;
  display: flex;
  margin-bottom: 10px;
  font-size: 16px;
}

.order-info p b {
  font-weight: 400;
  color: #9d9d9d;
  white-space: nowrap;
}

.study {
  padding: 25px 40px;
}

.study span {
  display: block;
  width: 140px;
  height: 42px;
  text-align: center;
  line-height: 42px;
  cursor: pointer;
  background: #ffc210;
  border-radius: 6px;
  font-size: 16px;
  color: #fff;
}
</style>